import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import {getMenu} from "../views/login.ts";

//批量导入
let modules=import.meta.glob("/src/views/module/**/*.vue");
console.log(modules);
let homeChildren: RouteRecordRaw[]=[];
//个性化加载Home的菜单路由
getMenu(0).forEach((item) => {
    if(item.module!==''){
        const route:RouteRecordRaw={
            path: item.path,
            name: item.name,
            component: modules[`/src/views/module/${item.module}.vue`]
        }
        homeChildren.push(route)
    }
});

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'login',
        component: () => import('/src/views/login.vue')
    },
    // 哪个页面有router-view标签，哪个页面就挂children
    {
        path: '/home',
        name: 'home',
        component: () => import('/src/views/home.vue'),
        children: homeChildren
    }
]


const router = createRouter({
    history: createWebHistory('/'),
    routes,
})

router.beforeEach((to,from) =>{
    const user=localStorage.getItem("user");
    if(to.path!=='/login' && user ===null) return '/login';
})

export default router

